<?php
class koudai_productModel extends model{
	public $base;
	public function __construct(&$base){
		parent::__construct($base);
		$this->base=$base;
		$this->table="koudai_product";
	}
	
	
	public function catlist(){
		return array(
			1=>"吃的",
			2=>"喝的",
			3=>"用的"
		);
	}
	
	public function id_list($option){
		$data=$this->select($option);
		if($data){
			foreach($data as $k=>$v){
				$t[$v['id']]=$v;
			}
			return $t;
		}
		return false;
	}
	
	public function GetProductIdsBySHop($option){
		$data=$this->select($option);
		if($data){
			foreach($data as $k=>$v){
				$t[$v['productid']]=$v;
			}
			return $t;
		}
		return false;
	}
	
	public function lsitByShopCat($shopid){
		$option=array(
			"where"=>" shopid=".$shopid
		);
		$data=$this->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M('product')->getListByIds($productids);
			foreach($data as $k=>$v){
				$v['title']=$products[$v['productid']]['title'];
				$v['imgurl']=$products[$v['productid']]['imgurl'];
				$v['price']=$products[$v['productid']]['price'];
				
				$cats[$v['catid']][]=$v;
			}
			return $cats;
		}
	}
	
	public function listbyshop($shopid){
		$option=array(
			"where"=>" shopid=".$shopid
		);
		$data=$this->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$productids[]=$v['productod'];
			}
			$products=M('product')->getListByIds($productids);
			foreach($data as $k=>$v){
				$v['title']=$products[$v['product']]['title'];
			}
		}
	}
	
	
	public function getlistbyshop($shopid){
		$option=array(
			"where"=>" shopid=".$shopid
		);
		$data=$this->select($option,$rscount);
		if($data){
			foreach($data as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M('product')->getListByIds($productids);
			foreach($data as $k=>$v){
				$v['title']=$products[$v['productid']]['title'];
				$data[$k]=$v;
			}
		}
		return $data;
	}
	
	
	public function getList($option,&$rscount=false){
	
		$data=$this->select($option,$rscount);
		 
		if($data){
			foreach($data as $k=>$v){
				$productids[]=$v['productid'];
			}
			$products=M('product')->getListByIds($productids);
			
			foreach($data as $k=>$v){
				$product=$products[$v['productid']];
				$v['title']=$products[$v['productid']]['title'];
				$v['imgurl']=$products[$v['productid']]['imgurl'];
				$v['market_price']=$product['market_price'];
				$v['lower_price']=$product['lower_price'];
				$data[$k]=$v;
			}
			
			return $data;
		}
	}
	
	public function statDayAdd($option){
		$option['kday']=$kday=date("Ymd",$option['time']);
		unset($option['time']);
		$where=" productid=".$option['productid']." AND k='".$option['k']."' AND kday='".$kday."' AND siteid=".$option['siteid']." AND shopid=".$option['shopid']." ";
		
		$row=M("stat_product_day")->selectRow($where);
		if($row){
			
			M("stat_product_day")->update(array(
				"ordernum"=>$row['ordernum']+$option['ordernum'],
				"money"=>$row['money']+$option['money'],
				"costmoney"=>$row['costmoney']+$option['costmoney'],
				"profitmoney"=>$row['profitmoney']+$option['profitmoney']
			),"id=".$row['id']);
		}else{
			M("stat_product_day")->insert($option);
		}		
		
	}
	
	public function statWeekAdd($option){
		$option['kweek']=$kweek=date("YW",$option['time']);
		unset($option['time']);
		$where=" productid=".$option['productid']." AND k='".$option['k']."' AND kweek='".$kweek."' AND siteid=".$option['siteid']." AND shopid=".$option['shopid']." ";
		
		$row=M("stat_product_week")->selectRow($where);
		if($row){
			
			M("stat_product_week")->update(array(
				"ordernum"=>$row['ordernum']+$option['ordernum'],
				"money"=>$row['money']+$option['money'],
				"costmoney"=>$row['costmoney']+$option['costmoney'],
				"profitmoney"=>$row['profitmoney']+$option['profitmoney']
			),"id=".$row['id']);
		}else{
			M("stat_product_week")->insert($option);
		}		
		
	}
	
	public function statMonthAdd($option){
		$option['kmonth']=$kmonth=date("Ym",$option['time']);
		unset($option['time']);
		$where=" productid=".$option['productid']." AND k='".$option['k']."' AND kmonth='".$kmonth."' AND siteid=".$option['siteid']." AND shopid=".$option['shopid']." ";
		
		$row=M("stat_product_month")->selectRow($where);
		if($row){
			
			M("stat_product_month")->update(array(
				"ordernum"=>$row['ordernum']+$option['ordernum'],
				"money"=>$row['money']+$option['money'],
				"costmoney"=>$row['costmoney']+$option['costmoney'],
				"profitmoney"=>$row['profitmoney']+$option['profitmoney']
			),"id=".$row['id']);
		}else{
			M("stat_product_month")->insert($option);
		}		
		
	}
	/*库存添加*/
	
	public function add($data){
		$this->insert($data);
		//同步口袋库存
		$kucun=M("kucun")->selectRow("koudai=".$data['shopid']);
		if($kucun){
			$kucun_product=M("kucun_product")->selectRow("shopid=".$kucun['kcid']." AND productid=".$data['productid']);
			if(empty($kucun_product)){
				M("kucun_product")->insert(array(
					"shopid"=>$kucun['kcid'],
					"productid"=>$data['productid'],
					"dateline"=>time(),
					"koudai"=>$data['shopid'],
				));
			}
			
		}
	}
	
	/**
	*口袋 库存处理
	*
		array(
			"shopid"=>$shopid,
			"productid"=>$v['productid']	,
			"total_num"=>$v['total_num'],
			"content"=>"您下了单产品[title]库存减少[total_num]"
		)
	*/
	public function total_num_add($option){
		$shopid=intval($option['shopid']);
		$productid=intval($option['productid']);
		$total_num=intval($option['total_num']);
		if(isset($option['content'])){
			$content=$option['content'];
		}else{
			$content="[title]库存变更[total_num]";
		}
		$row=M("koudai_product")->selectRow("shopid=".$shopid." AND productid=".$productid."");
	 	 
		$product=M("product")->selectRow("id=".$productid);
		M("koudai_product")->update(array(
			"total_num"=>$row['total_num']+$total_num
		),"id=".$row['id']);
		//同步口袋库存
		$kucun=M("kucun")->selectRow("koudai=".$shopid);
		if($kucun){
			M("kucun_product")->update(array(
				"total_num"=>$row['total_num']+$total_num
			),"shopid=".$kucun['kcid']." AND productid=".$productid);
			}
		//处理记录
		$content=str_replace(array(
			"[title]",
			"[total_num]"
		),array(
			$product['title'],
			$total_num
		),$content);
		M("koudai_kclog")->insert(array(
			"shopid"=>$shopid,
			"productid"=>$productid,
			"dateline"=>time(),
			"total_num"=>$total_num,
			"content"=>$content.",现在".($row['total_num']+$total_num)."",
		));
		
		
	}
	
	
}

?>